package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.GoodsDAO;
import com.wanmait.shoppingsys.dao.SpecialtyDAO;
import com.wanmait.shoppingsys.dao.TradeDAO;
import com.wanmait.shoppingsys.util.Pager;
import com.wanmait.shoppingsys.util.Rng;
import com.wanmait.shoppingsys.util.ShopCar;
import com.wanmait.shoppingsys.vo.Goods;
import com.wanmait.shoppingsys.vo.Specialty;
import com.wanmait.shoppingsys.vo.Trade;
import com.wanmait.shoppingsys.vo.UserInfo;

@WebServlet("/GoodsController")

public class GoodsController extends  HttpServlet {
	
	private SpecialtyDAO specialtyDAO=SpecialtyDAO.getDAO();
	private GoodsDAO goodsDAO = GoodsDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if(action==null) {
			return;
		}
	
		switch(action)
		{
		case "buy":
			this.buy(request, response);
			break;
		case "addCount":
			this.addCount(request, response);
			break;
		case "diffCount":
			this.diffCount(request, response);
			break;
		case "findAll":
			this.findAll(request, response);
			break;
		case "addOrder":
			this.addOrder(request, response);
			break;
		}
	}
	
	protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			response.sendRedirect(path+"/userinfo/login.jsp");
			return;
		}
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		String countStr = request.getParameter("count");
		Integer count = Integer.parseInt(countStr);
		Specialty specialty = specialtyDAO.findById(id);
		specialty.setCount(count);
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		if(shopCar==null) {
			shopCar = new ShopCar();
			request.getSession().setAttribute("shopCar", shopCar);
		
		}
		
		shopCar.addCommodity(specialty);
		response.sendRedirect(path+"/order/order.jsp");
	}
	
	protected void changeCount(HttpServletRequest request, HttpServletResponse response,int ccount) throws ServletException, IOException {

		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		
		if(shopCar==null) {
			shopCar = new ShopCar();

			request.getSession().setAttribute("shopCar", shopCar);
		}
		int count=shopCar.addCount(id, ccount);
		
		response.getWriter().print(count+","+shopCar.getTotalPrice());
	}
	
	protected void diffCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.changeCount(request, response, -1);
	
		
	}
	protected void addCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.changeCount(request, response, 1);
		
	}
	
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Pager pager = Pager.initb(request);
		 goodsDAO.findListById(pager);
		 request.getRequestDispatcher("/userinfo/userorder.jsp").forward(request,response);
	}
	
	protected void addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletContext().getContextPath();
		ShopCar shopCar = (ShopCar)request.getSession().getAttribute("shopCar");
		UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		        String addressa  =request.getParameter("address");
		        String[] parts = addressa.split(",");
		        String num = Rng.getRandomFileName();
		        Random random = new Random();
		        int rannum = (int)(random.nextDouble() * (999 - 100 + 1)) + 100;
		        ArrayList nums    = goodsDAO.getNum(num);
		        if(nums!=null)
		        {
		        	num=num+1+rannum;
		        }
		        goodsDAO.addOrder(shopCar, userinfo,parts,num);
		         Integer id  =  goodsDAO.find(num);
		         TradeDAO.getDAO().insert(shopCar, id);
		         
		         
		         ShopCarController  shopcar = new ShopCarController();
		         shopcar.clear(request, response);
		         response.sendRedirect(path);
		         
	}
	
	
	
	
	
	
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
